package com.atguigu.gmall.gateway.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.reactive.CorsConfigurationSource;
import org.springframework.web.cors.reactive.CorsWebFilter;
import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource;
import org.springframework.web.server.WebFilter;

/**
 * @author atguigu-mqx
 */
@Configuration
public class CorsConfig {

    //  跨域设置！
    //  <bean id="webFilter" class="org.springframework.web.server.WebFilter">
    //  </bean>
    @Bean
    public WebFilter webFilter(){

        //  创建对象
        CorsConfiguration corsConfiguration = new CorsConfiguration();
        corsConfiguration.setAllowCredentials(true);    //  跨域携带cookie
        corsConfiguration.addAllowedHeader("*");    //  允许携带请求信息
        corsConfiguration.addAllowedMethod("*");    //  运行各个方法
        corsConfiguration.addAllowedOrigin("*");    //  所有都可以跨域
        //  CorsConfigurationSource 创建实现类对象
        UrlBasedCorsConfigurationSource urlBasedCorsConfigurationSource = new UrlBasedCorsConfigurationSource();
        urlBasedCorsConfigurationSource.registerCorsConfiguration("/**",corsConfiguration);
        //  返回数据
        return new CorsWebFilter(urlBasedCorsConfigurationSource);
    }
}
